import facade
import isbnlib
from xjlibrary.our_file_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
configfile = BaseDir.get_new_path(curPath, "db.ini")


class TextToSql(object):

    def __init__(self):
        self.logger = facade.get_streamlogger()
        self.mysqlutils = facade.MysqlUtiles(configfile,
                                             "db",
                                             logger=self.logger)
        self.filepath = r"C:\Users\xuzhu\Downloads\part-00000"

    def coverlistinsert(self):
        count=0
        sql = "insert ignore into isbncover(isbn13,isbn10) values(%s,%s)"
        Listsql = []
        for line in BaseDir.read_file_r_mode_yield(self.filepath):
            isb = line.strip()
            isb = isbnlib.clean(isb)
            isbn10 = ''
            isbn13 = ''
            if (len(isb) == 10) and isbnlib.is_isbn10(isb):
                isbn10 = isb
                isbn13 = isbnlib.to_isbn13(isbn10)
            elif (len(isb) == 13) and isbnlib.is_isbn13(isb):
                isbn13 = isb
                isbn10 = isbnlib.to_isbn10(isbn13)
            if not isbn10:
                isbn10 = ''
            if not isbn13:
                isbn13 = ''

            if (not isbn10) and (not isbn13):
                continue


            # sql = sql.format(isbn13, isbn10)

            Listsql.append((isbn13, isbn10))
            if len(Listsql) > 500000:
                count = count + 500000
                self.mysqlutils.ExeSqlMany(sql,Listsql)
                Listsql.clear()
                print(count)
        if len(Listsql) > 0:
            count = count+len(Listsql)
            self.mysqlutils.ExeSqlMany(sql, Listsql)
            Listsql.clear()
            print(count)


if __name__ == "__main__":
    texttosql = TextToSql()
    texttosql.coverlistinsert()
